package apiCode;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.tmatesoft.svn.core.SVNException;

public class UpdateScheduler {

	private final ScheduledExecutorService scheduler =
		     Executors.newScheduledThreadPool(1);
	/**
	 * Run a scheduler that executes every X minutes
	 * Instantiate the commit method from Sink Class
	 */
	public void autoUpdate(){
		final Runnable runUpdate = new Runnable() {
			public void run() {
				ResultMediator mySink = new ResultMediator();
				mySink.connectToRepo();
				try {
					mySink.checkOut();
				} catch (SVNException e) {
					e.printStackTrace();
				}
				mySink.update();
			}
		};
		
		final ScheduledFuture<?> commitHandle = 
			scheduler.scheduleAtFixedRate(runUpdate, 10, 10, TimeUnit.SECONDS);
			scheduler.schedule(new Runnable() { 
				public void run () {
				System.out.println("Finished");
				commitHandle.cancel(true);	}
		},30,TimeUnit.SECONDS);
	}

}
